<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<link rel="stylesheet" href="/js/kindeditor/themes/default/default.css" />
<script src="/js/kindeditor/kindeditor-all-min.js"></script>
<script src="/js/kindeditor/lang/zh-CN.js"></script>

<form id="goods-add-form" method="post" style="padding: 10px;">  
	<p>   
        <label for="cid">商品分类:</label><span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <span id="cname"></span><span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <input class="easyui-validatebox" type="hidden" name="cid" 
         data-options="required:true,missingMessage:'分类不能为空'"/>
        <input type="button" value="选择分类" onclick="goods.selectGoodsCat();"/>
        <span style="color:red;">*</span>
    </p>
    <p>   
        <label for="title">标题:</label><span>&nbsp;&nbsp;&nbsp;&nbsp;</span>   
        <input class="easyui-validatebox" type="text" name="title" 
         data-options="required:true,missingMessage:'标题不能为空'"/>
        <span style="color:red;">*</span>
    </p>
     <p>   
        <label for="sellPoint">卖点:</label><span>&nbsp;&nbsp;&nbsp;&nbsp;</span>   
        <input class="easyui-validatebox" type="text" name="sellPoint" />
    </p>
     <p>   
        <label for="price">价格:</label><span>&nbsp;&nbsp;&nbsp;&nbsp;</span> 
		<input id="goods-add-form-goods-price" type="hidden" name="price" />  
        <input class="easyui-validatebox" type="number" name="pre-price" 
         data-options="required:true,missingMessage:'价格不能为空'" 
         onchange="var price = this.value*100; document.getElementById('goods-add-form-goods-price').value=price;"/>(单位：￥元，精确到分)
        <span style="color:red;">*</span>
    </p>
    <p>   
        <label for="number">库存:</label><span>&nbsp;&nbsp;&nbsp;&nbsp;</span>   
        <input class="easyui-validatebox" type="number" name="number" 
         data-options="required:true,missingMessage:'库存不能为空',validType:'number'"/>
        <span style="color:red;">*</span>
    </p>
    <p>   
        <label for="image">图片:</label><span>&nbsp;&nbsp;&nbsp;&nbsp;</span>   
        <input type="button" id="J_selectImage" value="批量上传" />
		<div id="J_imageView"></div>
		<input type="hidden" name="image" />
    </p>
</form>  

<script type="text/javascript">
	//初始化富文本编辑器
	var editor = KindEditor.editor({
		uploadJson : '/file/upload',
		allowFileManager : true
	});
	KindEditor('#J_selectImage').click(function() {
		//加载一个上传图片的插件
		editor.loadPlugin('multiimage', function() {
			editor.plugin.multiImageDialog({
				clickFn : function(urlList) {
					var div = KindEditor('#J_imageView');
					div.html('');
					var urls = '';
					KindEditor.each(urlList, function(i, data) {
						div.append('<img style="width:100px;height:100px" src="' + data.url + '">');
						//赋值image,有多个值，用“，”号分隔
						urls += data.url+',';
					});
					$("#goods-add-form input[name=image]").val(urls);
					editor.hideDialog();
				}
			});
		});
	});
	//选择商品分类
	goods.selectGoodsCat=function(){
		$('<div id="goods-add-cat-dialog"></div>').dialog({
			title:'选择商品类型',
			width:400,
			height:500,
		    modal:true,
		    collapsible:false,
		    minimizable:false,
		    maximizable:false,
		    closable:false,
		    content:'<ul id="cid-tree" class="easyui-tree" data-options="url:\'/goods/cat/tree\'"></ul>',
		    buttons:[{
				text:'选  择',
				iconCls:'icon-save',
				handler:function(){
					//判断是否选择了类型
					var node = $('#cid-tree').tree('getSelected');
					if(node == null){
						$.messager.alert('提示','请选择商品类型！','warning');
					}else{
						if($('#cid-tree').tree('isLeaf',node.target)){
							$('#goods-add-form input[name=cid]').val(node.id);
							$('#goods-add-form #cname').text(node.text);
							$('#goods-add-form #cid-tip').html('');
							$('#goods-add-cat-dialog').dialog('destroy');  
						}else{
							$.messager.alert('提示','请选择商品类型！','warning');
						}
					}
				}
			},{
				text:'关  闭',
				iconCls:'icon-cancel',
				handler:function(){
					$('#goods-add-cat-dialog').dialog('destroy');  
				}
			}]
		}); 
	}
	
	//商品添加
	goods.add=function(){
		$('#goods-add-form').form('submit', {
			url: '/goods/add',
			onSubmit: function(){
				// 返回false终止表单提交
				if(!$(this).form('validate'))
					return false;
				
				return true;	
			},
			success: function(msg){
				//把字符串转化成 json对象
				var msg = JSON.parse(msg);
				if(msg.code == 200){
					//添加成功后，更新表格
					$('#goods-list-dg').datagrid('reload');
					$.messager.alert('提示','添加成功！','info');
				}
			}
		});
	}
	
	
</script>